package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.UUID;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacResourceSysDao;
import org.scboy.rbac.service.ResourceSysService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 
 *  角色系统关联业务类
 * @author yanghao
 *
 */

@Service
public class ResourceSysServiceImp implements ResourceSysService {

	@Autowired
	EntityFactory factory;
	
	 /**
	   * 分页查询角色关联的系统
	   * @param model 查询参数
	   * @param limit 分页参数
	   * @return
	   */			
	@Override
	public Page page(Model model) {
		RbacResourceSysDao dao = factory.getDao(RbacResourceSysDao.class);
		return dao.queryPage(model);
	}
	
	 /**
	   * 新增角色系统关联
	   * @param resId 资源ID
	   * @param sysId 系统ID
	   * @return
	   */		
	@Override
	public boolean add(String resId, String sysId) {
		RbacResourceSysDao dao = factory.getDao(RbacResourceSysDao.class);
		Model oldModel=dao.get(Model.create().add("resId", resId).add("sysId", sysId));
		if(oldModel!=null)return false;
		Model model=Model.create().add("id", UUID.randomUUID().toString())
				.add("resId", resId)
				.add("sysId", sysId)
				.add("addTime", new Date())
				.add("editTime", new Date());
		int result=dao.insert(model);
		if(result>0){
			return true;
		}else{
			return false;
		}
	}
	
	 /**
	   * 根据ID删除角色系统关联
	   * @param id ID
	   * @return
	   */		
	@Override
	public boolean delete(String id) {
		RbacResourceSysDao dao = factory.getDao(RbacResourceSysDao.class);
		int result=dao.delete(Model.create().add("id", id));
		if(result>0){
			return true;
		}else{
			return false;
		}
	}

}
